#include "AuctioneerMgr.h"
#include "base/core/utility.h"
#include "LogicService.h"
#include "ProtoBuffer/AllProtocol.h"
#include "base/core/com_define.h"
#include "Character/PlayerMgr.h"
#include "base/core/log.h"
#include "Common/Item/ItemMgr.h"
#include "Common/TableData/FunctionunlockFunctionUnlockCfg.h"

CAuctioneerMgr::CAuctioneerMgr()
{

}

CAuctioneerMgr::~CAuctioneerMgr()
{

}

bool CAuctioneerMgr::Init()
{
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}

	pMessageDispatch->registerServerHandler(EMODULE_ID_AUCTIONEER, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_AUCTIONEER, this);
	return true;
}

void CAuctioneerMgr::UnInit()
{
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return ;
	}

	pMessageDispatch->unregisterServerHandler(EMODULE_ID_AUCTIONEER);
	pMessageDispatch->unregisterClientHandler(EMODULE_ID_AUCTIONEER);
	return;
}

void CAuctioneerMgr::AddTestItemToDB(CharIDType playerCid)
{
	LogicToCenterAuctioneerSale sendMsg;
	sendMsg.set_charid(playerCid);
	//找到该玩家的物品
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(playerCid);
	if (pPlayer == nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find player failed...charid=%u", playerCid);
		return;
	}

	PackagePart * pPart = dynamic_cast<PackagePart *>(pPlayer->GetPart(PART_PACKAGE));
	if (pPart == nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find PackagePart failed...charid=%u", playerCid);
		return;
	}

	//for (uint32_t i = 0; i < 1; ++i)
	{
		const CItemBase * pItemBase = pPart->GetItem(2);
		//MMOLOG_PROCESS_ERROR(pItemBase!=nullptr);

		if (pItemBase)
		{
			AuctioneerItemDBData * pdata = sendMsg.mutable_iteminfo();
			ItemProtoInfo  *itemdata = pdata->mutable_data();
			//pPart->SetUpdateItemInfo((CItemBase *)pItemBase, itemdata);
			g_GetItemMgr()->SetItemProtoInfo((CItemBase *)pItemBase, itemdata);
			pdata->set_globelid(0);  //这个要到数据库中设置
			pdata->set_saleid(pPlayer->GetCid());
			pdata->set_salename(pPlayer->GetName());
			pdata->set_saletime(Time::Now().UnixSec());
			pdata->set_validtime(1);
			pdata->set_price(100);
			pdata->set_moneytype(1);
			//sendMsg.set_allocated_iteminfo(&pdata);
			//pdata.set_allocated_data(&itemdata);
			g_GetLogicService()->SendDataToCenter(EMODULE_ID_AUCTIONEER, LOGIC_TO_CENTER_AUCTIONEER_SALE_ITEM, &sendMsg);
		}
	}
	

}

void CAuctioneerMgr::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	switch (actionId)
	{
	case CENTER_TO_LOGIC_AUCTIONEER_REDUCE_MONEY:
		OnReduceMoney(package);
		break;
	case CENTER_TO_LOGIC_AUCTIONEER_SALE_ITEM_RESULT:
		OnSaleItemResult(package);
		break;
	default:
		break;
	}
	return;
}
//
//
//void CAuctioneerMgr::onDBProxyMessage(uint32_t actionId, RECV_PACKAGE &package)
//{
//	return;
//}
//
void CAuctioneerMgr::onClientMessage(uint32_t actionId, RECV_PACKAGE& package, uint64_t Id /* = 0 */)
{
	const FunctionunlockFunctionUnlockCfgInfo * pConfig = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_AUCTIONEER);
	if (!pConfig)
	{
		LogErrPrint("CAuctioneerMgr::onClientMessage cannt find FunctionUnlock_ID_TYPE_AUCTIONEER config");
		return;
	}
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(Id);
	if (pPlayer == nullptr)
	{
		LogErrFmtPrint("CAuctioneerMgr::onClientMessage cannt find player:%lu", Id);
		return;
	}

	if (pPlayer->GetPAttrValue(P_LEVEL) < pConfig->unlockLevel)
	{
		LogErrFmtPrint("CAuctioneerMgr::onClientMessage unlocklevel playerLev:%d, unlockLev:%d", pPlayer->GetPAttrValue(P_LEVEL), pConfig->unlockLevel);
		return;
	}

	switch (actionId)
	{
	case CLIENT_TO_LOGIC_AUCTIONEER_SALE_ITEM: //出售物品
		OnSaleItem(package);
		break;
	default:
		break;
	}
	return;
}


void CAuctioneerMgr::OnSaleItem(RECV_PACKAGE &package)
{
	AuctioneerSaleItemReq req;
	AuctioneerSaleItemRsp rsp;
	LogicToCenterAuctioneerSale sendMsg;
	MAP_UINT16_INT32 tmpmap;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem return but parse failed...charid=%u", req.charid());
		return;
	}
	rsp.set_index(req.index());
	//找到该玩家的物品
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(req.charid());
	if (pPlayer==nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find player failed...charid=%u", req.charid());
		return;
	}

	sendMsg.set_charid(req.charid());

	PackagePart * pPart = dynamic_cast<PackagePart *>(pPlayer->GetPart(PART_PACKAGE));
	if (pPart==nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find PackagePart failed...charid=%u", req.charid());
		return;
	}

	const CItemBase * pItemBase=pPart->GetItem(req.index());
	//MMOLOG_PROCESS_ERROR(pItemBase!=nullptr);

	if(!pItemBase)
	{
	   rsp.set_retcode(RET_PACKAGE_ITEM_NOT_EXITS);
	   pPlayer->SendDataToClientEx(EMODULE_ID_AUCTIONEER, LOGIC_TO_CLIENT_AUCTIONEER_SALE_RESULT, &rsp);
	}
	else if (!g_GetItemMgr()->CanTrade(pItemBase->GetItemID(),pItemBase->GetBind()))
	{
		//不能交易
		rsp.set_retcode(RET_PACKAGE_ITEM_CAN_NOT_TRADE);
		pPlayer->SendDataToClientEx(EMODULE_ID_AUCTIONEER, LOGIC_TO_CLIENT_AUCTIONEER_SALE_RESULT, &rsp);
	}
	else
	{
		AuctioneerItemDBData * pdata = sendMsg.mutable_iteminfo();
		ItemProtoInfo* pItemDate = pdata->mutable_data();
		g_GetItemMgr()->SetItemProtoInfo((CItemBase *)pItemBase, pItemDate, req.itemnum());
		//藏宝图强制取消激活
		pItemDate->set_active(0);
		//pPart->SetUpdateItemInfo((CItemBase *)pItemBase, pItemDate);
		pdata->set_globelid(0);
		pdata->set_saleid(pPlayer->GetCid());
		pdata->set_salename(pPlayer->GetName());
		pdata->set_saletime(Time::Now().UnixSec());
		pdata->set_validtime(req.hour());
		pdata->set_price(req.price());
		pdata->set_moneytype(req.type());
		
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_AUCTIONEER, LOGIC_TO_CENTER_AUCTIONEER_SALE_ITEM, &sendMsg);
	
		//rsp.set_retcode(retCode);
	}
	
	
//	return;
//Exit0:
	//通知客户端
//	pPlayer->SendDataToClientEx(EMODULE_ID_AUCTIONEER, LOGIC_TO_CLIENT_AUCTIONEER_SALE_RESULT, &rsp);
	//return;
}


void CAuctioneerMgr::OnSaleItemResult(RECV_PACKAGE &package)
{
	CenterToLogicAuctioneerSaleResult req;
	AuctioneerSaleItemRsp rsp;
	LogicToCenterAuctioneerSale sendMsg;
	MAP_UINT16_INT32 tmpmap;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItemResult return but parse failed...charid=%u", req.charid());
		return;
	}
	rsp.set_index(req.itemindex());
	rsp.set_retcode(req.retcode());
	//找到该玩家的物品
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(req.charid());
	if (pPlayer == nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find player failed...charid=%u", req.charid());
		return;
	}

	PackagePart * pPart = dynamic_cast<PackagePart *>(pPlayer->GetPart(PART_PACKAGE));
	if (pPart == nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find PackagePart failed...charid=%u", req.charid());
		return;
	}

	if (req.retcode() == RET_SUCCESS)
	{
		//先销毁,然后通知成功
		tmpmap[req.itemindex()] = req.itemnum();
		SItemSourceParam sourceParam;
		sourceParam.source = S_ACTUINEER;
		pPart->RemoveItem(tmpmap, sourceParam);
	}
	
	pPlayer->SendDataToClientEx(EMODULE_ID_AUCTIONEER, LOGIC_TO_CLIENT_AUCTIONEER_SALE_RESULT, &rsp);
}


void CAuctioneerMgr::OnReduceMoney(RECV_PACKAGE &package)
{
//	uint32_t retCode = 0;
	CenterToLogicAuctioneerBuy req;
	LogicToCenterAuctioneerBuyResult rsp;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem return but parse failed...charid=%u", req.charid());
		return;
	}

	rsp.set_itemid(req.itemid());
	rsp.set_itemnum(req.itemnum());
	rsp.set_charid(req.charid());

	//判断钱够不够扣
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(req.charid());
	if (pPlayer == nullptr)
	{
		MMOLOG_FMT_ERROR("[logic] OnSaleItem find player failed...charid=%u", req.charid());

		rsp.set_retcode(RET_LOGIN_CHARACTER_NOT_EXITS);
	}
	else
	{
		if (req.type() == CenterToLogicAuctioneerBuy::en_gold)
		{
			if ((uint64_t)pPlayer->GetAttr(C_GOLD) < req.allmoney())
			{
				rsp.set_retcode(RET_GOLD_LACK);
			}
			else
			{
				rsp.set_retcode(0);
				//pPlayer->AddAttr(C_GOLD, -1 * req.allmoney(), true);
				pPlayer->AddSourceAttr(C_GOLD, -1 * req.allmoney(), true, S_ACTUINEER);
			}
		}
		else
		{
			if ((uint32_t)pPlayer->GetAttr(C_DIAMOND) < req.allmoney())
			{
				rsp.set_retcode(RET_DIAMOND_LACK);
			}
			else
			{
				rsp.set_retcode(0);
				//pPlayer->AddAttr(C_DIAMOND, -1 * req.allmoney(), true);
				pPlayer->AddSourceAttr(C_DIAMOND, -1 * req.allmoney(), true, S_ACTUINEER);
			}
		}
	}

	playerTradeEvent tradeEvent;
	tradeEvent.type = req.type();
	tradeEvent.Num = -1 * req.allmoney();
	g_GetEvent()->FireExecute(EVENT_ADD_TITLE, req.charid(), CREATURE_PLAYER, (playerTradeEvent*)&tradeEvent, sizeof(playerTradeEvent));

	g_GetLogicService()->SendDataToCenter(EMODULE_ID_AUCTIONEER, LOGIC_TO_CENTER_AUCTIONEER_REDUCE_MONEY_RESULT, &rsp);
}
